package com.wss.common.widget;

import ohos.agp.components.*;
import ohos.app.Context;

/**
 * TODO Describe：可添加任意ViewGroup的RadioGroup
 * Created by 吴天强 on 2019/3/18.
 */
public class StrongRadioGroup extends DirectionalLayout {

    private int mCheckedId = -1;
    private RadioContainer.CheckedStateChangedListener mChildOnCheckedChangeListener;
    private boolean mProtectFromCheckedChange = false;
    private OnCheckedChangeListener mOnCheckedChangeListener;
//    private PassThroughHierarchyChangeListener mPassThroughListener;

    public StrongRadioGroup(Context context) {
        super(context);
        setOrientation(VERTICAL);
        init();
    }

    public StrongRadioGroup(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
        init();
    }

    public StrongRadioGroup(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        init();
    }

    private void init() {
        mChildOnCheckedChangeListener = new CheckedStateTracker();
//        mPassThroughListener = new PassThroughHierarchyChangeListener();
    }

    @Override
    public void addComponent(Component child, int index, ComponentContainer.LayoutConfig params) {
        if (child instanceof RadioButton) {
            final RadioButton button = (RadioButton) child;
            if (button.isChecked()) {
                mProtectFromCheckedChange = true;
                if (mCheckedId != -1) {
//                    setma
                }
            }
        }
        super.addComponent(child, index, params);
    }

    /**
     * 查找radioButton控件
     */
    public RadioButton findRadioButton(ComponentContainer group) {
        RadioButton resBtn = null;
        int len = group.getChildCount();
        for (int i = 0; i < len; i++) {
            if (group.getComponentAt(i) instanceof RadioButton) {
                resBtn = (RadioButton) group.getComponentAt(i);
            } else if (group.getComponentAt(i) instanceof ComponentContainer) {
                findRadioButton((ComponentContainer) group.getComponentAt(i));
            }
        }
        return resBtn;
    }

    public void check(int id) {
        if (id != -1 && (id == mCheckedId)) {
            return;
        }

        if (mCheckedId != -1) {
            setCheckedStateForView(mCheckedId, false);
        }

        if (id != -1) {
            setCheckedStateForView(id, true);
        }

        setCheckedId(id);
    }

    private void setCheckedId(int id) {
        mCheckedId = id;
        if (mOnCheckedChangeListener != null) {
            mOnCheckedChangeListener.onCheckedChanged(this, mCheckedId);
        }
    }

    private void setCheckedStateForView(int viewId, boolean checked) {
        Component checkedView = findComponentById(viewId);
        if (checkedView != null && checkedView instanceof RadioButton) {
            ((RadioButton) checkedView).setChecked(checked);
        }
    }

    public int getCheckedRadioButtonId() {
        return mCheckedId;
    }

    public void clearCheck() {
        check(-1);
    }

    public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
        mOnCheckedChangeListener = listener;
    }

    @Override
    public ComponentContainer.LayoutConfig createLayoutConfig(Context context, AttrSet attrSet) {
        return new LayoutParams(context, attrSet);
    }

    public static class LayoutParams extends DirectionalLayout.LayoutConfig {
        public LayoutParams(Context context, AttrSet attrSet){
            super(context, attrSet);
        }

    }

    /**
     * <p>
     * Interface definition for a callback to be invoked when the checked radio
     * button changed in this group.
     * </p>
     */
    public interface OnCheckedChangeListener {
        /**
         * <p>
         * Called when the checked radio button has changed. When the selection
         * is cleared, checkedId is -1.
         * </p>
         *
         * @param group     the group in which the checked radio button has changed
         * @param checkedId the unique identifier of the newly checked radio button
         */
        public void onCheckedChanged(StrongRadioGroup group, int checkedId);
    }

    private class CheckedStateTracker implements
            RadioContainer.CheckedStateChangedListener {

        @Override
        public void onCheckedChanged(RadioContainer radioContainer, int index) {
            if (mProtectFromCheckedChange) {
                return;
            }

            mProtectFromCheckedChange = true;
            if (mCheckedId != -1) {
                setCheckedStateForView(mCheckedId, false);
            }
            mProtectFromCheckedChange = false;
            int id = index;
            setCheckedId(id);
        }
    }
}
